package com.longrise.android.byjk.plugins.vip.integralexchange;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/7/22.
 * 积分兑换 展业秀背景的adapter
 */

public class IExchangezyxbgAdapter extends BaseQuickAdapter<EntityBean, BaseViewHolder> {

    private final String TAG = "IntegralExchangeAdapter";
    private final String mName;

    List<EntityBean> mList = new ArrayList<>();
    private String mGoodstype;
    private OnIntegralExchangeRcvItemClickListener mListener;

    public IExchangezyxbgAdapter(String name) {
        super(R.layout.item_rcv_integralexchangebg, null);
        this.mName = name;
    }

    public void setDatas(EntityBean[] beans, String goodstype) {
        this.mGoodstype = goodstype;
        mList.clear();
        for (int x = 0; x < beans.length; x++) {
            mList.add(beans[x]);
        }
        setNewData(mList);
    }


    @Override
    protected void convert(final BaseViewHolder helper, EntityBean item) {
        View convertView = helper.getConvertView();
        ImageView iv = (ImageView) convertView.findViewById(R.id.item_rcv_integralexchangebg_iv);
        ImageView iv2 = (ImageView) convertView.findViewById(R.id.item_rcv_integralexchangebg_iv2);
        TextView tv = (TextView) convertView.findViewById(R.id.item_rcv_integralexchangebg_tv);
        final String needscores = item.getString("needscores",""); //所需花费的积分
        final String cdnurl = item.getString("showpicurl"); //商品图片地址
        final String backpicurl = item.getString("backpicurl"); //大图片地址
        final String lockstate = item.getString("state"); //解锁状态  1:已解锁  0：未解锁 2:正在使用
        final String goodsId = item.getString("goodsid");
        final String detailpicurl = item.getString("detailpicurl"); //预览图片
        final String areaname = item.getString("areaname",""); //商品的名称 用于谷歌分析
        //.placeholder
        Glide.with(mContext).load(cdnurl).transform(new GlideRoundTransform(mContext)).into(iv);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tv.getLayoutParams();
        String textmsg = "";
        switch (lockstate) {
            case "0":  //0未解锁
                iv2.setVisibility(View.GONE);
                textmsg = "使用" + needscores + "积分兑换";
                layoutParams.setMargins(0, AppUtil.dip2px(18),0,0);
                layoutParams.height = AppUtil.dip2px(48);
                tv.setBackgroundResource(R.drawable.bt_coin_fill);
                tv.setTextColor(Color.parseColor("#FFFFFF"));
                tv.setClickable(true);
                break;
            case "1":  //1:已解锁
                iv2.setVisibility(View.GONE);
                textmsg = "立即使用";
                layoutParams.setMargins(0, AppUtil.dip2px(20),0,0);
                layoutParams.height = AppUtil.dip2px(36);
                tv.setBackgroundResource(R.drawable.bg_integral_exchange2_tv);
                tv.setTextColor(Color.parseColor("#FFA200"));
                tv.setClickable(true);
                break;
            case "2":  //2:正在使用
                iv2.setVisibility(View.VISIBLE);
                textmsg = "使用中";
                layoutParams.setMargins(0, AppUtil.dip2px(20),0,0);
                layoutParams.height = AppUtil.dip2px(36);
                tv.setBackgroundResource(R.drawable.bg_integral_exchange2_tv);
                tv.setTextColor(Color.parseColor("#FFA200"));
                tv.setClickable(false);
                break;
        }
        tv.setLayoutParams(layoutParams);
        tv.setText(textmsg);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(1, lockstate, goodsId, mGoodstype, needscores, null, null, helper.getPosition());
                    addGoogleEvent(lockstate,areaname);
                }
            }
        });
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(2, lockstate, goodsId, mGoodstype, needscores, backpicurl, detailpicurl, helper.getPosition());
                }
            }
        });
    }

    public void setOnRcvItemClickListener(OnIntegralExchangeRcvItemClickListener listener) {
        this.mListener = listener;
    }

    public void addGoogleEvent(String lockstate, String areaname) {
        switch (lockstate) {
            case "0":
                addGoogleEvent(mName,"兑换",areaname);
                break;
            case "1":
                addGoogleEvent(mName,"立即使用",areaname);
                break;
        }
    }

    private void addGoogleEvent(String par1, String par2, String par3) {
        Tracker_Google.getInstance().addEvent(par1, par2, par3);
    }
}
